﻿using System;
using System.Collections;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Application.MainBoundedContext.Setting;

namespace Huirui.Cavan.Application.MainBoundedContext.PurchaseModule
{
    #region Purchase

    public class PurchaseAppService : BaseAppService<Purchase, string, PurchaseSearch, IPurchaseRepository>, IPurchaseAppService
    {
        private IStockInOutDetailAppService StockInDetailAppService { get; set; }
        private IPurchaseOrderAppService PurchaseOrderAppService { get; set; }
        private IStockInOutTypeAppService StockInOutTypeAppService { get; set; }
        private IUnPayAppService UnPayAppService { get; set; }
        private IStockAppService StockAppService { get; set; }
        private IStockInOutAppService StockInOutAppService { get; set; }
        private IWareHouseAppService WareHouseAppService { get; set; }
        private ISupplierAppService SupplierAppService { get; set; }

        public override BoolGenericResult<Purchase> AddOrUpdate(Purchase entity)
        {
            Init();
            AddPurchaseResult result = new AddPurchaseResult();
            this.PurchaseOrderAppService = this.SpringContext.GetObject(PurchaseModuleConst.PURCHASEORDERAPPSERVICE) as IPurchaseOrderAppService;
            this.UnPayAppService = this.SpringContext.GetObject(PurchaseModuleConst.UNPAYAPPSERVICE) as IUnPayAppService;
            this.StockInOutTypeAppService = this.SpringContext.GetObject(StockModuleConst.STOCKINOUTTYPEAPPSERVICE) as IStockInOutTypeAppService;
            this.StockAppService = this.SpringContext.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
            this.StockInOutAppService = this.SpringContext.GetObject(StockModuleConst.STOCKINOUTAPPSERVICE) as IStockInOutAppService;
            this.WareHouseAppService = this.SpringContext.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;
            this.SupplierAppService = this.SpringContext.GetObject(SystemModuleConst.SUPPLIERAPPSERVICE) as ISupplierAppService;

            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Purchaser purchaser = new Purchaser(employee);

            PurchaseOrder purchaseOrder = null;
            if (entity.PurchaseOrder.IsNotNull())
            {
                purchaseOrder = this.PurchaseOrderAppService.GetById(entity.PurchaseOrder.Id);

                if (HuiruiProject.UserSettings.PurchaseOrderShouldBeChecked && purchaseOrder.CheckedStatus != SystemIdentifier.CheckedStatus.Pass)
                {
                    result.IsSuccess = false;
                    result.Messages.Add("采购订单需先审核");
                    return result;
                }
            }
            entity.WareHouse = this.WareHouseAppService.GetById(entity.WareHouse.Id);
            entity.Supplier = this.SupplierAppService.GetById(entity.Supplier.Id);
            StockInOutType stockInOutType = this.StockInOutTypeAppService.GetById(3);

            foreach (StockInOutDetail siod in entity.StockInOut.StockInOutDetails)
            {
                siod.Stock = this.StockAppService.GetById(siod.Stock.Id);
                siod.WareHouse = entity.WareHouse;
            }


            IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { purchaseOrder.Corporation }) as IIDGenerateAppService;
            string purchaseCode = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.Purchase);
            result = purchaser.Purchase(purchaseCode, entity.PurchaseDate, entity.Project, entity.Supplier, entity.ReceivingNumber, entity.Currency, entity.CurrencyRate, entity.TaxRate,
                entity.SupplierInvoiceNumber, entity.DeliveryAddress, entity.Remark, entity.InvoiceDescription, entity.Discount, SystemIdentifier.PayType.Cash, PurchaseIdentifier.PayCondition.None,
                entity.PayDeadline, entity.WareHouse, string.Empty, string.Empty, employee, purchaseOrder, entity.StockInOut, stockInOutType);
            if (result.IsSuccess)
            {
                result.Object.StockInOut.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.StockIn);
                result.UnPay.UnPayCode = result.Object.Id;
                this.StockInOutAppService.Add(result.Object.StockInOut);
                this.Repository.Save(result.Object);
                foreach (StockInOutDetail siod in result.Object.StockInOut.StockInOutDetails)
                {
                    this.StockAppService.Update(siod.Stock);
                }
                this.PurchaseOrderAppService.Update(purchaseOrder);
                this.UnPayAppService.Add(result.UnPay);

                result.Messages.Add("入库成功");
            }

            return result;
        }
    }
    #endregion
}